package cn.gaotu.system.controller;

import cn.gaotu.basic.util.AjaxResult;
import cn.gaotu.basic.util.PageList;
import cn.gaotu.system.domain.SystemDictionaryDetail;
import cn.gaotu.system.query.SystemDictionaryDetailQuery;
import cn.gaotu.system.service.SystemDictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionaryDetail")
public class SystemDictionaryDetailController {
    @Autowired
    private SystemDictionaryDetailService systemDictionaryDetailService;

    // http://localhost:8080/systemDictionaryDetail/1 Get
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public SystemDictionaryDetail findById(@PathVariable("id")Long id){
        return systemDictionaryDetailService.findById(id);
    }

    // http://localhost:8080/systemDictionaryDetail Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<SystemDictionaryDetail> findAll(){
        return systemDictionaryDetailService.findAll();
    }

    // http://localhost:8080/systemDictionaryDetail/1 Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            systemDictionaryDetailService.DeleteDep(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
    // http://localhost:8080/systemDictionaryDetail Put 参数：{} - 请求体
    @PutMapping
    //@RequestBody SystemDictionaryDetail systemDictionaryDetail - 从请求体中获取数据赋值给SystemDictionaryDetail对象
    //这个是添加或者更新
    @ApiOperation(value = "更新或者保存")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        System.out.println(systemDictionaryDetail);
        try {
            if (systemDictionaryDetail.getId() == null) {
                systemDictionaryDetailService.save(systemDictionaryDetail);
            } else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }
    // http://localhost:8080/ PosystemDictionaryDetailst 参数：{} - 请求体
    //  查询分页
    @PostMapping
    @ApiOperation(value = "查询页面")
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery) {
        return systemDictionaryDetailService.queryPage(systemDictionaryDetailQuery);
    }
    //批量删除
    @PatchMapping
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            systemDictionaryDetailService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

}
